//Nathan Popham
//4003-573-01: Procedural Shading
//Lab 4 
//
//mybrick.frag
// Textures a surface with a pattern of 1x1, 1x2, and 2x2 blocks. A predefined
// 5x5 pattern of these blocks is repeated across the surface.
//

varying vec3 pos;

varying vec3 N;

int TwoDIndex(float x, float y, float width) { return int(x + y * width); }

void main()
{
    //unfortunately have to hardcode the array
    //GLSL doesn't seem to have inline array initialization
    //(actually it DOES, grad student told me how but this is already done)
    //a 3.0 represents a tile with top and left mortar border
    //a 2.0 is a tile with only top mortar border
    //a 1.0 is a tile with only left mortar border
    //a 0.0 is a tile with no mortar borders
    float brickPattern[25];
    brickPattern[0] = 3.0;
    brickPattern[1] = 2.0;
    brickPattern[2] = 3.0;
    brickPattern[3] = 3.0;
    brickPattern[4] = 2.0;
    brickPattern[5] = 3.0;
    brickPattern[6] = 3.0;
    brickPattern[7] = 1.0;
    brickPattern[8] = 3.0;
    brickPattern[9] = 2.0;
    brickPattern[10] = 1.0;
    brickPattern[11] = 3.0;
    brickPattern[12] = 2.0;
    brickPattern[13] = 3.0;
    brickPattern[14] = 3.0;
    brickPattern[15] = 3.0;
    brickPattern[16] = 2.0;
    brickPattern[17] = 3.0;
    brickPattern[18] = 1.0;
    brickPattern[19] = 3.0;
    brickPattern[20] = 1.0;
    brickPattern[21] = 0.0;
    brickPattern[22] = 3.0;
    brickPattern[23] = 2.0;
    brickPattern[24] = 1.0;

    vec3 bc1 = vec3(.5, .5, .8);
    vec3 bc2 = vec3(.8, .5, .5);
    vec3 bc3 = vec3(.5, .8, .5);
    int brickColors[25];
    brickColors[0] = 1;
    brickColors[1] = 1;
    brickColors[2] = 2;
    brickColors[3] = 3;
    brickColors[4] = 3;
    brickColors[5] = 2;
    brickColors[6] = 3;
    brickColors[7] = 2;
    brickColors[8] = 1;
    brickColors[9] = 1;
    brickColors[10] = 2;
    brickColors[11] = 1;
    brickColors[12] = 1;
    brickColors[13] = 3;
    brickColors[14] = 2;
    brickColors[15] = 1;
    brickColors[16] = 1;
    brickColors[17] = 2;
    brickColors[18] = 3;
    brickColors[19] = 2;
    brickColors[20] = 1;
    brickColors[21] = 1;
    brickColors[22] = 3;
    brickColors[23] = 3;
    brickColors[24] = 2;


     //float tilewidth = 2.0;
    //float tileheight = 2.0;
    float tilewidth = 1.0;
    float tileheight = 1.0;
    float mortarwidth = 0.2;
    vec3 mortarcolor = vec3 (0.4, 0.4, 0.4);
    vec3 brickcolor = vec3(0.5, 0.5, 0.8);
    float lacunity = 2.0;
    float gain = 0.8; 

    //float numTilesWidth = 5.0;
    //float numTilesHeight = 5.0;
    float numTilesWidth = 5.0;
    float numTilesHeight = 5.0;
    

    
    float is = gl_TexCoord[0].s;
    float it = gl_TexCoord[0].t;

    float brickColumn = floor(is*numTilesWidth - .0001);
    float brickRow = floor(it*numTilesHeight - .0001);
    float brickx = mod(is*numTilesWidth*tilewidth, tilewidth);
    float bricky = mod(it*numTilesHeight*tileheight, tileheight);
    
    float borderInfo = brickPattern[TwoDIndex(brickColumn, brickRow, numTilesWidth)];
    
    float vec3Noise = 1.0;
    
//    if(octaves > 0.0)
//        vec3Noise = turbulence(P, octaves, lacunity, gain);
   
    vec3 finalColor;

    if((borderInfo == 2.0 || borderInfo == 0.0 || brickx > mortarwidth) && (borderInfo == 1.0 || borderInfo == 0.0 || bricky > mortarwidth))
{
    int colorID = brickColors[TwoDIndex(brickColumn, brickRow, numTilesWidth)];
    if(colorID == 1)
        finalColor = bc1;
    else if(colorID == 2)
        finalColor = bc2;
    else
        finalColor = bc3;
}
else
{
    finalColor = mortarcolor;
}

    //gl_FragColor = (ambient() + diffuse(N)) *
    //     mix(mortarcolor, brickcolor * vec3Noise,  * Os;


    gl_FragColor = vec4 (finalColor, 1.0); // noise4 (pos);
}
